package com.qiche.controller.mgr;

import com.github.pagehelper.PageInfo;
import com.qiche.dto.VoucherDto;
import com.qiche.model.StoreMember;
import com.qiche.model.User;
import com.qiche.model.Voucher;
import com.qiche.model.VoucherRepository;
import com.qiche.service.StoreMemberService;
import com.qiche.service.VoucherRepositoryService;
import com.qiche.service.VoucherService;
import com.qiche.util.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代金券管理控制器
 * Created by Linzw on 2018/01/24.
 */
@Api(value = "/vouchers", description = "代金券管理控制器")
@RestController
@RequestMapping("/vouchers")
public class VoucherController {
    @Autowired
    private VoucherService voucherService;
    @Autowired
    private StoreMemberService storeMemberService;

    @ApiOperation(value = "根据条件和分页获取代金券列表")
    @GetMapping
    public Map<String, Object> getAll(Voucher voucher, @RequestParam(required = false, defaultValue = "1") int page,
                                      @RequestParam(required = false, defaultValue = "10") int limit) throws Exception {
        Map<String, Object> map = new HashMap<>();
        User currentUser=UserUtils.getCurrentUser();
        if(currentUser.getMerchantId() == null){
            return null;
        }
        voucher.setMerchantId(currentUser.getMerchantId());
        PageInfo<Voucher> pageInfo = voucherService.selectByPage(voucher, page, limit);
        List<VoucherDto> dtoList=new ArrayList<>();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        pageInfo.getList().forEach(voucher1 -> {
            StoreMember storeMember=storeMemberService.selectByKey(voucher1.getStoreMemberId());

            VoucherDto dto=new VoucherDto();
            if(storeMember!=null){
                dto.setPhone(storeMember.getPhone());
            }
            dto.setId(voucher1.getId());
            dto.setTitle(voucher1.getTitle());
            dto.setAmount(voucher1.getAmount());
            dto.setCode(voucher1.getCode());
            dto.setIsWriteoff(voucher1.getIsWriteoff());
            dto.setWriteOffTime(voucher1.getWriteOffTime());
            dto.setCreateTime(voucher1.getCreateTime());
            if(voucher1.getIndefinite()==1){
                dto.setDueTime("长期有效");
            }else if(voucher1.getIndefinite()==0){
                dto.setDueTime(simpleDateFormat.format(voucher1.getDueTime()));
            }
            dtoList.add(dto);
        });

        map.put("code", 0);
        map.put("msg", "查询成功");
        map.put("count", pageInfo.getTotal());
        map.put("data", dtoList);
        return map;
    }



}
